<?php
// 首页显示信息以及基本配置
namespace Partner\Controller;
use Think\Controller;
class CrowdfundingController extends CommonController{
   function index(){
       $id=session('u_id');
       $store_info=M('store')->where("uid=".$id)->find();
       if($store_info==null){
           $this->error('您暂时没有店铺，请前去添加',U('Store/shopadd'));
       }

       $crowdfunding=M('crowdfunding');
       $crowdfundinglist=$crowdfunding->select();
       $shop   =   M('shop');
       foreach ($crowdfundinglist as $key=>$val){
             if($val["store_id"]==null){
               $goods_id=$val["goods_id"];
               $shop_info=$shop->field('id,title,store_id')->where("id=$goods_id")->find();
               $crowdfundinglist[$key]['product_name']=$shop_info['title'];
               $data['id']=$val["id"];
               $data['store_id']=$shop_info["store_id"];
               M('crowdfunding')->save($data);
             }
       }
       $user_info=M('partner')->where('u_id='.$id)->find();
       $b_ids=explode(',',$user_info['b_id']);
       $count=$crowdfunding->where('store_id='.$store_info['id'])->count();
       $Page = new \Think\Page ($count,14); // 实例化分页类 传入总记录数
       $Page->setConfig('header','条记录');
       $Page->setConfig('prev','上一页');
       $Page->setConfig('next','下一页');
       $show = $Page->show(); // 分页显示输出
       $crowdfundinglist=$crowdfunding
           ->field('yt_shop.title,yt_crowdfunding.*')
            ->join('yt_shop on yt_shop.id=yt_crowdfunding.goods_id')
            ->where('yt_crowdfunding.store_id='.$store_info['id'])
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();

       $this->assign('page',$show);
       $this->assign('count',$count);
       $this->assign('b_ids',$b_ids);
       $this->assign('crowdfundinglist',$crowdfundinglist);
       $this->display();
    }

    public function getsearchgoodslist(){
        $id=session('u_id');
        $store_info=M('store')->where("uid=".$id)->find();
        $shop   =   D('shop');
        $where ='ishidden=0 and store_id='.$store_info['id'];
        $title  =   preg_replace('/\//',' ',I('get.title'));
        if( $title ){
            $titleArray =   explode(' ',$title);
            if( $titleArray ){
                foreach ( $titleArray AS $key=>$row ){
                    $where  .=  $row != ''?' AND title LIKE "%'.$row.'%"':'';
                }
            }
        }
        $where  .=  I('get.id') !== ''?' AND id = '.I('get.id').'':'';
        $array  =   $shop->field("id,title,price,image,num")->where($where)->order('id DESC')->limit(8)->select();
        $data['data']=$array;
        $this->ajaxReturn($data);
    }

    public function add(){

        if($_POST){
            $uid=session('u_id');
            $store_info=M('store')->where("uid=".$uid)->find();
            $data=I('post.');
            $crowdfunding=M('crowdfunding');
            $shop   =   M('shop');
            //开启事物
            $crowdfunding->startTrans();
            $shop->startTrans();
            $add_info['uid']=$uid;
            $add_info['project_name']=$data['project_name'];
            $add_info['store_id']=$store_info['id'];
            $add_info['total_num']=$data['total_num'];
            $add_info['total_money']=$data['total_money'];
            $add_info['days']=$data['days'];
            $add_info['c_type']=1;
            $add_info['c_status']=-1;
            $add_info['start_time']=$data['start_time'];
            $add_info['end_time']=$data['end_time'];
            $add_info['create_time']=date('Y-m-d H:i:s',time());
            $add_info['update_time']=date('Y-m-d H:i:s',time());

            if($data['start_time']<=$add_info['create_time']){
                $add_info['state']=1;
            }
            if($add_info['start_time']>$add_info['end_time']){
                echo "0|||众筹活动已开始截止时间不能早于开始时间|||";
                die;
            }
            //print_r($data['ids']);
            $ids=explode(',',$data['ids']);
            foreach($ids as $key=>$value){
                $add_info['goods_id']=$value;
                $shop_list=$crowdfunding->where(array('goods_id'=>$value))->find();
                if($shop_list){
                    echo "0|||该商品已参加众筹，请重新选择商品";
                    die;
                }
            }


            $crowdfunding_id=$crowdfunding->add($add_info);
            if($crowdfunding_id){
                $shop->where('id='.$add_info['goods_id'])->setField(array('is_crowdfunding'=>'1','crowdfunding_id'=>$crowdfunding_id));
                $crowdfunding->commit();
                $shop->commit();
                echo "0|||添加成功|||/Crowdfunding/index/";
            }else{
                $crowdfunding->rollback();
                $shop->rollback();
                echo "0|||添加失败|||/Crowdfunding/add/";
            }
        }else{
            $this->display();
        }

    }

    function edit(){
        $uid=session('u_id');
        $id=I('get.id');
        $crowdfunding=M('crowdfunding');
        $crowdfunding_info=$crowdfunding->where("uid=$uid and id=$id and c_type=1")->find();

        $shop   =   M('shop');
        $goods_id=$crowdfunding_info["goods_id"];

        $shop_info=$shop->field('yt_shop.id,yt_shop.title,yt_shop.num,yt_shop.image,yt_shop.price')->where("id=$goods_id")->select();
        $ids[0]=$shop_info[0]["id"];
        $shop_info=json_encode($shop_info);
        $ids=json_encode($ids);
        //print_r($ids);print_r($shop_info);
        $this->assign('ids',$ids);

        $this->assign('shop_info',$shop_info);
        $this->assign('crowdfunding_info',$crowdfunding_info);
        $this->display();
    }

    /**
     * 获取众筹详情
     */
    public function getCrowdfundingInfo()
    {
        $coupon_type=M('coupon_type');
        $id = $_POST['id'];
        $crowdfunding=M('crowdfunding');
        $data=$crowdfunding->where("id=$id")->find();
        $shop   =   M('shop');
        $goods_id=$data["goods_id"];
        $shop_info=$shop->field('yt_shop.id,yt_shop.title')->where("id=$goods_id")->find();
        $data['product_name']=$shop_info['title'];

        $this->ajaxReturn($data);

    }

    function update(){
        if($_POST){
            $uid=session('u_id');
            $data=I('post.');
            $id=$data['id'];
            $crowdfunding=M('crowdfunding');
            $shop   =   M('shop');
            //开启事物
            $crowdfunding->startTrans();
            $shop->startTrans();
            $crowdfunding_info=$crowdfunding->where("uid=$uid and id=$id")->find();
            //已支持数
            if($crowdfunding_info["num"]>$data['total_num']){
                echo "0|||已被".$crowdfunding_info["num"]."人支持，更新支持数不能低于".$crowdfunding_info["num"]."人|||";
                die;
            }





            $add_info['project_name']=$data['project_name'];
            $add_info['total_num']=$data['total_num'];
            $add_info['total_money']=$data['total_money'];
            $add_info['days']=$data['days'];
            $add_info['start_time']=$data['start_time'];
            $add_info['end_time']=$data['end_time'];
            $add_info['update_time']=date('Y-m-d H:i:s',time());
            if($data['start_time']<=$add_info['create_time']){
                $add_info['state']=1;
            }else{
                $add_info['state']=0;
            }
            if($add_info["start_time"]>$add_info['end_time']){
                echo "0|||众筹活动已开始截止时间不能早于开始时间|||";
                die;
            }



            $crowdfunding_id=$crowdfunding->where('id='.$id)->save($add_info);

            if($crowdfunding_id){
                $crowdfunding->commit();
                $shop->commit();
                echo "0|||修改成功|||/Crowdfunding/index/";
            }else{
                $crowdfunding->rollback();
                $shop->rollback();
                echo "0|||修改失败|||";
            }
        }
    }

    // 删除众筹
    function delete(){
        if(isset($_GET['id'])){
            $crowdfunding=M('crowdfunding');
            $shop   =   M('shop');
            //开启事物
            $crowdfunding->startTrans();
            $shop->startTrans();
            $id = I("get.id",0,'intval');
            $crowdfunding_info=$crowdfunding->where("id=$id")->find();
            $shop->where('id='.$crowdfunding_info['goods_id'])->setField(array('is_crowdfunding'=>'0','crowdfunding_id'=>null));
            $crowdfunding_id=$crowdfunding->where('id='.$id)->delete();
            if ($crowdfunding_id) {
                $crowdfunding->commit();
                $shop->commit();
                $this->ajax_r ( 1,'删除成功 ',array('url' => U('Crowdfunding/index')));
            } else {
                $crowdfunding->rollback();
                $shop->rollback();
                $this->ajax_r ( 0,'删除失败 ');
            }
        }
    }


}